package cn.hill4j.tool.spring.ext.feign.wrap;

import feign.Request;
import feign.RequestTemplate;
import feign.Target;

import java.util.function.Supplier;

/**
 * ClassName: DymTarget
 * Description: TODO
 *
 * @author hillchen
 * Date: 2023年08月13日
 * @version: 1.0
 */
public class WrapTarget<T> implements Target<T> {
    private Class<T> type;
    private String name;
    private Supplier<String> urlGetter;

    public WrapTarget(Class<T> type, String name, Supplier<String> urlGetter) {
        this.type = type;
        this.name = name;
        this.urlGetter = urlGetter;
    }

    @Override
    public Class<T> type() {
        return type;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String url() {
        return urlGetter.get();
    }

    @Override
    public Request apply(RequestTemplate input) {
        if (input.url().indexOf("http") != 0) {
            input.target(url());
        }
        return input.request();
    }
}
